<template>
  <div class="picker-year">
    <el-form ref="year-range-picker" :model="formData" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item prop="yearStart">
            <el-date-picker
              class="picker-first"
              v-model="formData.yearStart"
              value-format="yyyy"
              @change="val=>onDateChange(0,val)"
              :picker-options="pickerOptionsStart"
              :placeholder="startPlaceholder"
              type="year">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">至</el-col>
        <el-col :span="11">
          <el-form-item prop="yearEnd">
            <el-date-picker
              class="picker-second"
              v-model="formData.yearEnd"
              value-format="yyyy"
              @change="val=>onDateChange(1,val)"
              :picker-options="pickerOptionsEnd"
              :placeholder="endPlaceholder"
              type="year">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import moment from 'moment'

export default {
  name: "YearRangePicker",
  data() {
    return {
      formData: {
        yearStart: '',
        yearEnd: '',
      },
      rules: {
        yearStart: [
          {required: true, message: '请选择开始年份', trigger: 'change'}
        ],
        yearEnd: [
          {required: true, message: '请选择结束年份', trigger: 'change'}
        ],
      },
      pickerOptionsStart: {
        disabledDate(date) {
          return false
        },
      },
      pickerOptionsEnd: {
        disabledDate(date) {
          return false
        },
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldValue) {
        if (newVal) {
          this.yearStart = newVal[0]
          this.yearEnd = newVal[1]
        }
      },
      immediate: true
    },
  },
  props: {
    value: {
      type: Array,
      default: []
    },
    startPlaceholder: {
      type: String,
      default: ''
    },
    endPlaceholder: {
      type: String,
      default: ''
    }
  },
  methods: {
    onDateChange(index, value) {
      this.pickerOptionsStart = {
        disabledDate: (date) => {
          if(this.yearEnd !=='')
          {
            return (moment(date) > moment().year(this.yearEnd))
          }else{
            return false
          }

        },
      }
      this.pickerOptionsEnd = {
        disabledDate: (date) => {
          if(this.yearStart !== '')
          {
            return moment(date) <= moment().year(this.yearStart)
          }else{
            return false
          }

        },
      }
      if (!value) {
        this.rules = {
          yearStart: [
            {required: false, message: '请选择开始年份', trigger: 'change'}
          ],
          yearEnd: [
            {required: false, message: '请选择结束年份', trigger: 'change'}
          ],
        }
        this.formData = {
          yearStart: '',
          yearEnd: ''
        }
      } else {
        this.rules = {
          yearStart: [
            {required: true, message: '请选择开始年份', trigger: 'change'}
          ],
          yearEnd: [
            {required: true, message: '请选择结束年份', trigger: 'change'}
          ],
        }
      }
      this.$refs["year-range-picker"].validate((valid) => {
        // if (valid) {
          console.log('校验结果', index, value, [this.formData.yearStart, this.formData.yearEnd])
          this.$emit('change', [this.formData.yearStart, this.formData.yearEnd])
          this.$emit('input', [this.formData.yearStart, this.formData.yearEnd])
        // } else {
        //   console.log('error submit!!');
        //   return false;
        // }
      });

    }
  },
}
</script>

<style scoped>
.picker-year {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  //width: 60%;
}

.picker-first {
  width: 100%;
}

.line {
  text-align: center;
}

.picker-second {
  width: 100%;
}
</style>
